package com.example.chesswithnet.Dao;

import com.example.chesswithnet.Entity.User;
import com.example.chesswithnet.db.DBConnect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDao {

    public enum registerEnum{
        ACCOUNT_EXIST,
        SUCCESS,
        ERROR
    }

    /**
     * 登录
     * @param account 账号
     * @param pass 密码
     * @return
     */
    public User login(String account, String pass){
        Connection connection = new DBConnect().getCount();
        String sql = "select * from users where account = ? and pass = ?";
        User bean = null;
        // 创建Statement来处理
        try {
            // sql预处理，即将上面的sql中的?替换成真正的值
            PreparedStatement ps = connection.prepareStatement(sql);
            // 处理参数，即替换?
            ps.setObject(1,account);
            ps.setObject(2,pass);
            ResultSet res = ps.executeQuery();  // 执行查询sql，并返回结果集
            while (res.next()){
                bean = new User();
                bean.setAccount(res.getNString("account"));
                bean.setPassword(res.getNString("pass"));
                bean.setPhone(res.getNString("phone"));
            }
            connection.close();
            return bean;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 注册
     * @param account 账号
     * @param pass 密码
     * @param phone 手机号
     * @return
     * @throws SQLException
     */
    public registerEnum register(String account, String pass, String phone) throws SQLException {
        Connection connection = new DBConnect().getCount();
        String sql = "insert into users(account,pass,phone,create_date) values ( ?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setObject(1,account);
        ps.setObject(2,pass);
        ps.setObject(3,phone);
        ps.setObject(4,new Date());
        int res = ps.executeUpdate(); // 如果成功则返回成功的数量，否则返回-1
        connection.close();
        if (res == -1) return registerEnum.ERROR;
        return registerEnum.SUCCESS;
    }

    /**
     * 判断账号是否存在
     * @param account
     * @return  registerEnum.SUCCESS：可以注册  registerEnum.ACCOUNT_EXIST：账号已存在
     * @throws SQLException
     */
    public registerEnum isExistByAccount(String account) throws SQLException {
        Connection connection = new DBConnect().getCount();
        String sql = "select * from users where account = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setObject(1,account);
        ResultSet res = ps.executeQuery(sql);
        //wasNULL()方法来判断接受到的SQL查询值是否为NULL，该方法返回值为Boolean类型
        connection.close();
        if(res.wasNull()) return registerEnum.SUCCESS;
        return registerEnum.ACCOUNT_EXIST;
    }

    /**
     * 忘记密码
     * @param account
     * @param phone
     * @return
     */
    public User forgetPass(String account,String phone) throws SQLException {
        Connection conn = new DBConnect().getCount();
        String sql = "select * from users where account = ? and phone = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1,account);
        ps.setObject(2,phone);
        ResultSet res = ps.executeQuery();
        User bean = null;
        while (res.next()){
            bean = new User();
            bean.setAccount(res.getNString("account"));
            bean.setPassword(res.getNString("pass"));
            bean.setPhone(res.getNString("phone"));
        }
        conn.close();
        return bean;
    }

    public static List<User> userList = new ArrayList<>();

    //保存登陆的账号
    public static User user;


    //遍历账号是否存在
    public static boolean userListSelectAccount(String account){
        for (User item : userList)
            if(item.getAccount().equals(account)) return true;
        return false;
    }

//    找回密码
//    public static User forgetPass(String account,String phone){
//        for(User item : userList)
//            if(item.getAccount().equals(account) && item.getPhone().equals(phone))
//                return item;
//        return null;
//
//    }
}
